package com.tuo51.childcare.service.impl;

import com.tuo51.childcare.domain.expertroom.ExpertHint;
import com.tuo51.childcare.domain.expertroom.ExpertRoom;
import com.tuo51.childcare.domain.feed.Feeds;
import com.tuo51.childcare.repository.ExpertRoomRepository;
import com.tuo51.childcare.service.ExpertRoomService;
import com.tuo51.childcare.service.FeedsService;
import com.tuo51.evaluate.domain.evaluatingV2.EvaluateV2Record;
import com.tuo51.core.domain.org.Organization;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.core.repositories.OrgnizationRepository;
import com.tuo51.evaluate.repositories.evaluatev2.EvaluateV2RecordRepository;
import com.tuo51.core.service.StaffService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExpertRoomServiceImpl implements ExpertRoomService {

    @Autowired
    private EvaluateV2RecordRepository recordRepository;

    @Autowired
    private OrgnizationRepository orgRepository;

    @Autowired
    private FeedsService feedsService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private ExpertRoomRepository roomRepository;

    @Override
    public ExpertHint forStaff(Staff staff) {
        return null;
    }


    @Override
    public ExpertHint generateHintByReport(Staff staff, EvaluateV2Record report) {

        String reportId = null ;

        if (report != null){
            reportId = report.getId();
        }else{
            //找到用户最近的报告 id
            Feeds feeds = feedsService.getLatestFeeds(staff.getId(), FeedsService.FeedsType.EvaluateRecord);
            if (feeds != null) {
                reportId = feeds.getLinkId();
            }
        }

        if (StringUtils.isNotEmpty(reportId)){
            EvaluateV2Record record = recordRepository.getOne(reportId);

            Organization org = null;
            if (StringUtils.isNotEmpty(record.getOrgId())){
                org = orgRepository.getOne(record.getOrgId());
            }else {
                String teacherId = record.getCreateTeacherId();
                Staff teacher = staffService.getById(teacherId);
                org = staffService.staffOrgnization(teacher);
            }

            String hint = "首都儿研所李教授提醒您";
            String orgName = org.getName();
            if (StringUtils.isEmpty(orgName)){
                orgName = "专家";
            }
            String subHint = "亲爱的爸爸妈妈：" + orgName + "为" + record.getChildrenName() + "宝贝做了一次儿心量表测评，宝贝在大运动区...";
            ExpertHint guide = ExpertHint.of(1, reportId,
                    hint, subHint,
                    "https://e.res.tuo51.com/public/static/temp/renwu.png", false);

            return guide;
        }else{
            String hint = "首都儿研所李教授提醒您";
            String subHint = "亲爱的爸爸妈妈，为了更全面的了解您宝宝的成长发育，给宝贝预约专家做一次全面的儿心量表测评吧！";
            ExpertHint guide = ExpertHint.of(2, null,
                    hint, subHint,
                    "https://e.res.tuo51.com/public/static/temp/renwu.png", false);

            return guide;
        }
    }

    @Override
    public ExpertRoom forReport(String reportId){
        return roomRepository.getFirstBy();
    }

    @Override
    public ExpertRoom forId(String roomId) {
        return roomRepository.getOne(roomId);
    }
}
